package UI;
import java.awt.Container;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import thesis.Concept;



public class ConceptSelectionDialog extends JDialog implements ActionListener{
	private static final long serialVersionUID = 1L;
	
	private List<Concept> concepts = null;

	private JCheckBox conceptSelections[] = null;
	
	private JButton ok = null;
	
	public ConceptSelectionDialog(List<Concept> concepts){
		this.concepts = concepts;
		
		setBounds(0, 0, GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width, GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height);
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		
		int numberOfConcepts = concepts.size();
		
		Container c = this.getContentPane();
		c.setLayout(new GridLayout(3 + numberOfConcepts, 1));
		
		JPanel panelTitle = new JPanel();
		panelTitle.setLayout(new GridLayout(1,1));
		JLabel title = new JLabel("NEWS");
		Font fontTitle = new Font("Arial", Font.BOLD, 40);
		title.setFont(fontTitle);
		title.setHorizontalAlignment(JLabel.CENTER);
		panelTitle.add(title);
		
		c.add(panelTitle);
		
		JPanel panelSubtitle = new JPanel();
		panelSubtitle.setLayout(new GridLayout(1,1));
		JLabel subTitle = new JLabel("Select concepts of interest");
		Font fontSubtitle = new Font("Arial", Font.BOLD, 25);
		subTitle.setFont(fontSubtitle);
		subTitle.setHorizontalAlignment(JLabel.CENTER);
		panelSubtitle.add(subTitle);
		
		c.add(panelSubtitle);
		
		conceptSelections = new JCheckBox[numberOfConcepts];
		int currentConcept = 0;
		
		for (Concept concept : concepts){
			JPanel panelConcept = new JPanel();
			panelConcept.setLayout(new GridLayout(1, 2));
			
			JPanel panelConceptSelection = new JPanel();
			
			conceptSelections[currentConcept] = new JCheckBox();
			conceptSelections[currentConcept].setSelected(true);
			
			JLabel conceptName = new JLabel(concept.getId());
			Font fontConcName = new Font("Arial", Font.BOLD, 25);
			conceptName.setFont(fontConcName);
			
			panelConceptSelection.add(conceptSelections[currentConcept]);
			panelConceptSelection.add(conceptName);
			
			JPanel panelKeywords = new JPanel();
			JTextArea keywordsArea = new JTextArea();
			keywordsArea.setEditable(false);
			Font fontTextArea = new Font("Arial", Font.PLAIN, 20);
			keywordsArea.setFont(fontTextArea);
			int keywordsCount = 0;
			
			for(String keyword : concept.getKeywords()){
				keywordsArea.append(keyword + ",\t");
				keywordsCount++;
				
				if(keywordsCount % 5 == 0){
					keywordsArea.append("\n");
				}
			}
			panelKeywords.add(keywordsArea);
			
			panelConcept.add(panelConceptSelection);
			panelConcept.add(panelKeywords);
			
			c.add(panelConcept);
			
			currentConcept++;
		}
		
		JPanel panelButtonOk = new JPanel();
		ok = new JButton("Ok");
		ok.addActionListener(this);
		panelButtonOk.add(ok);
		
		c.add(panelButtonOk);
	}


	public void actionPerformed(ActionEvent e) {
		List<Concept> notSelectedConcepts = new ArrayList<Concept>();
			
		boolean oneConceptSelected = false;
			
		for (int i=0; i<concepts.size(); i++){
			if (!conceptSelections[i].isSelected()){
				notSelectedConcepts.add(concepts.get(i));
			}
			else{
				oneConceptSelected = true;
			}
		}
		if(oneConceptSelected){
			for (Concept notSelectedConcept : notSelectedConcepts){
				concepts.remove(notSelectedConcept);
			}
			this.dispose();
		}
		else{
			JOptionPane.showMessageDialog(this, "Select at least one concept", "Error", JOptionPane.ERROR_MESSAGE);
		}

	}
}
